package com.clgc.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clgc.eduservice.entity.CourseTeacher;
import com.clgc.eduservice.entity.EduCourse;
import com.clgc.eduservice.service.CourseTeacherService;
import com.clgc.eduservice.service.EduCourseService;

import com.clgc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;


/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@Api("课程管理模块")
@RestController
@RequestMapping("/eduservice/edu-course")
public class EduCourseController {
    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    CourseTeacherService courseTeacherService;

    @ApiOperation("新增课程")
    @PostMapping("/addCourse/{id}")
    public R c_addCourse(@ApiParam("课程信息") @RequestBody EduCourse eduCourse, @ApiParam("教师id") @PathVariable("id") Integer id) {
        boolean flag = eduCourseService.save(eduCourse);

        CourseTeacher courseTeacher = new CourseTeacher();
        courseTeacher.setCourseId(eduCourse.getId());
        courseTeacher.setTeacherId(id);

        boolean save = courseTeacherService.save(courseTeacher);
        return flag && save ? R.ok() : R.error().message("添加课程失败");
    }

    //修改课程
    @ApiOperation("修改课程")
    @PostMapping("/updateCourse")
    public R c_update(@RequestBody EduCourse eduCourse) {
        boolean flag = eduCourseService.updateById(eduCourse);
        return flag ? R.ok() : R.error().message("修改课程失败");

    }

    //逻辑删除课程
    @ApiOperation("删除课程")
    @DeleteMapping("deleteCourseById/{id}")
    public R c_delete(@PathVariable String id) {
        boolean flag = eduCourseService.removeById(id);
        return flag ? R.ok() : R.error().message("删除操作失败");
    }

    //根据老师id查询下面对应的所有课程
    @ApiOperation("查询课程")
    @GetMapping("/selectCourse/{id}/{current}/{size}")
    public R c_select(@PathParam("教师id") @PathVariable("id") int id, @ApiParam("当前页码") @PathVariable("current") long current, @ApiParam("每页显示行数") @PathVariable("size") long size) {
        Page<EduCourse> page = eduCourseService.s_selectCourse(id, current, size);
        return R.ok().data("coursePage", page);
    }


}

